<?php

namespace app\api\Service;

use app\common\library\Auth;
use app\common\model\Area;
use think\Config;

class ShopService
{
    // 获取轮播图
    public static function getBanner()
    {
        $list = \app\admin\model\Banner::where(['type'=>2,'status'=>'normal'])
            ->order('weigh desc')->field('id,image,content')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>$list];
    }

    // 获取导航
    public static function getNavigation()
    {
        $list = \app\admin\model\ShopNavigation::where(['status'=>'normal'])
            ->order('weigh desc')->field('id,name')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>$list];
    }

    // 获取商品列表
    public static function getGoodsList($data)
    {
//        $page = isset($data['page'])? $data['page'] : 1;
        $limit = isset($data['limit'])? $data['limit'] : 10;
        $navigation_id = $data['navigation_id'];
        if(!$navigation_id) return ['code' =>0,'msg'=>'无效参数','data'=>''];

        $list = \app\admin\model\ShopGoods::where(['navigation_id'=>$navigation_id,'status'=>'normal'])->order('weigh desc')->field('id,name,image,paid_price,original_price,suggest_price,label')->paginate($limit)->each(function($item){
            $item['image'] = cdnurl($item['image'],true);
        });
        return ['code' =>1,'msg'=>'查询成功','data'=>$list];
    }

    // 获取推荐列表
    public static function getRecommendList($data)
    {
        $limit = isset($data['limit'])? $data['limit'] : 4;
        $list = \app\admin\model\ShopGoods::where(['status'=>'normal'])->orderRaw('rand()')->field('id,name,image,paid_price,original_price,suggest_price,label,sales')->limit($limit)->select();
        if($list){
            foreach($list as &$item){
                $item['image'] = cdnurl($item['image'],true);
            }
        }
        return ['code' =>1,'msg'=>'查询成功','data'=>$list];
    }

    // 获取商品详情
    public static function getGoodsDetails($data)
    {
        $goods_id = $data['goods_id'];
        if(!$goods_id) return ['code' =>0,'msg'=>'无效参数','data'=>''];

        $details = \app\admin\model\ShopGoods::where(['id'=>$goods_id,'status'=>'normal'])->field('id,name,images,paid_price,original_price,suggest_price,label,describe,content')->find();
        if($details){
            // $details['image'] = cdnurl($details['image'],true);
            $images = explode(',',$details['images']);
            foreach($images as &$item){
                $item = cdnurl($item,true);
            }
            $details['images'] = $images;
        }else{
            $details = [];
        }
        return ['code' =>1,'msg'=>'查询成功','data'=>$details];
    }

}
